﻿create table #FrameworkSchemas ( SchemaName varchar(50) );

insert into #FrameworkSchemas values ('OLibrary'), ('FrameworkInternals'), ('String'), ('Int'), ('BigInt'), ('SmallInt'), 
                                     ('TinyInt'), ('Date'), ('Time'), ('DateTime'), ('Guid'), ('Base64'), ('File'),
                                     ('Assert'), ('PerformanceMonitoring'), ('SqlVariant'), ('Table'), ('Test')

/* Drop all the routines (stored procedures and functions) contained in the schemas in the framework */
declare @DropAllRoutinesSQL nvarchar(max) = '';
select @DropAllRoutinesSQL = @DropAllRoutinesSQL + 'drop ' + ROUTINE_TYPE + QuoteName( ROUTINE_SCHEMA ) + '.' + QuoteName( ROUTINE_NAME ) + ';' 
  from INFORMATION_SCHEMA.ROUTINES 
 where ROUTINE_SCHEMA in ( select SchemaName from #FrameworkSchemas );
execute( @DropAllRoutinesSQL );
go

/* Drop all the views contained in the schemas in the framework */
declare @DropAllViewsSQL nvarchar(max) = '';
select @DropAllViewsSQL = @DropAllViewsSQL + 'drop view ' + QuoteName( TABLE_SCHEMA ) + '.' + QuoteName( TABLE_NAME ) + ';' 
  from INFORMATION_SCHEMA.VIEWS 
 where TABLE_SCHEMA in ( select SchemaName from #FrameworkSchemas );
execute( @DropAllViewsSQL );
go

/* Drop all the schemas in the framework */
declare @DropAllSchemasSQL nvarchar(max) = '';
select @DropAllSchemasSQL = @DropAllSchemasSQL + 'drop schema ' + QuoteName( SCHEMA_NAME ) + ';' 
  from INFORMATION_SCHEMA.SCHEMATA 
 where SCHEMA_NAME in ( select SchemaName from #FrameworkSchemas );
execute( @DropAllSchemasSQL );
go

drop table #FrameworkSchemas
go

sp_configure 'Show Advanced Options', 1
reconfigure
go

sp_configure 'Ad Hoc Distributed Queries', 1
reconfigure
go